﻿using System.Text;

namespace MyTRCP.Common.Models
{
    public class BssSettings : BitWalkableObj
    {
        public byte P1 { get; set; }//bit size 4
        public byte P2 { get; set; }//bit size 4
        public bool P3 { get; set; }
        public bool P4 { get; set; }
        public bool P5 { get; set; }
        public bool P6 { get; set; }
        public bool P7 { get; set; }
        public bool P8 { get; set; }
        public bool P9 { get; set; }
        //skip 1bit   value=0
        public byte P10 { get; set; }//bit size 4
        //skip 4bit   value=0
        public byte P11 { get; set; }//bit size 8
        public ulong P12 { get; set; }//bit size 32 , low 32 bit
        public byte[] P13 { get; set; } = new byte[12];//bit size 8*12
        public byte[] P14 { get; set; } = new byte[18];//bit size 8*18
        public string P15 { get; set; } = "/$";  //bit size 8*2
        public byte[] P16 { get; set; } = new byte[6];//bit size 8*6
        //bit size 32,p12 high 32 bit.
        public BssSettings(byte[] data) : base(data)
        {
        }

        public override BitWalker WriteToBitWalker(BitWalker bw)
        {
            bw.WriteBits(P1, 4)
                .WriteBits(P2, 4)
                .WriteBit(P3)
                .WriteBit(P4)
                .WriteBit(P5)
                .WriteBit(P6)
                .WriteBit(P7)
                .WriteBit(P8)
                .WriteBit(P9)
                .WriteBit(false)
                .WriteBits(P10, 4)
                .WriteBits(0, 4)
                .WriteBits(P11, 8)
                .WriteBits((int)P12, 32);
            for (int i = 0; i < P13.Length; i++)
            {
                bw.WriteBits(P13[i], 8);
            }
            for (int i = 0; i < P14.Length; i++)
            {
                bw.WriteBits(P14[i], 8);
            }
            bw.WriteBits(P15[0], 8).WriteBits(P15[1], 8);
            for (int i = 0; i < P16.Length; i++)
            {
                bw.WriteBits(P16[i], 8);
            }
            bw.WriteBits((int)(P12 >> 32), 32);
            return bw;
        }

        public override BitWalker ReadFromBitWalker(BitWalker bw)
        {
            if (bw.ByteLen < 46)
                throw new ArgumentException("字节数组长度过小（46）", nameof(bw));
            P1 = (byte)bw.ReadBits(4);
            P2 = (byte)bw.ReadBits(4);
            P3 = bw.ReadBit();
            P4 = bw.ReadBit();
            P5 = bw.ReadBit();
            P6 = bw.ReadBit();
            P7 = bw.ReadBit();
            P8 = bw.ReadBit();
            P9 = bw.ReadBit();
            bw.SkipBits(1);
            P10 = (byte)bw.ReadBits(4);
            bw.SkipBits(4);
            P11 = (byte)bw.ReadBits(8);
            P12 = bw.ReadBits(32);
            //P13 = new byte[12];
            for (int i = 0; i < P13.Length; i++)
            {
                P13[i] = (byte)bw.ReadBits(8);
            }
            //P14 = new byte[18];
            for (int i = 0; i < P14.Length; i++)
            {
                P14[i] = (byte)bw.ReadBits(8);
            }
            StringBuilder sb = new StringBuilder();
            sb.Append((char)bw.ReadBits(8));
            sb.Append((char)bw.ReadBits(8));
            P15 = sb.ToString();
            //P16 = new byte[6];
            for (int i = 0; i < P16.Length; i++)
            {
                P16[i] = (byte)bw.ReadBits(8);
            }
            if (bw.ByteLen >= 50)
                P12 = (ulong)bw.ReadBits(32) << 32 | P12 & 4294967295uL;
            return bw;
        }
    }
}